package com.tian.scroll.pull.views

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.RecyclerView
import com.tian.scroll.R


/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/11/30 11:28
 */
class NestScrollViewRecyclerView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
    NestedScrollView(context, attrs, defStyleAttr) {

    private val TAG = "tiansongtao"
    private var mContext: Context
    private lateinit var recyclerView: RecyclerView
    private lateinit var contentView: View
    private var contentHeight = 0

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null)


    init {
        mContext = context
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        recyclerView = findViewById(R.id.recycler_view)
        contentView = findViewById(R.id.rv_header)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val params = recyclerView.layoutParams as ViewGroup.LayoutParams
        params.height = MeasureSpec.getSize(heightMeasureSpec)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        contentHeight = contentView.measuredHeight
        Log.i(TAG, "onMeasure:  contentHeight=$contentHeight")
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
    }

    override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
        Log.i(TAG, "onNestedPreScroll: dy=$dy scrolly=${scrollY}")
        if (dy < 0 && scrollY == 0 && recyclerView.computeVerticalScrollOffset() == 0) {
            //下滑
            dispatchNestedScroll(0, 0, 0, dy, null)
        } else {
            //上滑
            if (scrollY < contentHeight) {
                if (scrollY + dy < contentHeight) {
                    scrollBy(0, dy)
                    consumed[1] = dy
                } else if (scrollY + dy > contentHeight) {
                    val scrollViewNeedScrollY = contentHeight - scrollY
                    scrollBy(0, scrollViewNeedScrollY)
                    consumed[1] = scrollViewNeedScrollY
                }
            }
        }

    }

    override fun fling(velocityY: Int) {
        val dy = FlingUtil.getDistanceByVelocity(mContext, velocityY)
        if (scrollY < contentHeight) {
            if (scrollY + dy <= contentHeight) {
                super.fling(velocityY)
            } else if (scrollY + dy > contentHeight) {
                val scrollViewNeedScrollY = contentHeight - scrollY
                //让NestedScrollView先处理所有的滚动事件
                val scrollViewNeedVelocity = FlingUtil.getVelocityByDistance(mContext, scrollViewNeedScrollY.toDouble())
                if (velocityY > 0) {
                    super.fling(scrollViewNeedVelocity)
                } else {
                    super.fling(-scrollViewNeedVelocity)
                }
                //把剩余的滚动事件交给RecyclerView处理
                val recyclerViewScrollY = dy - scrollViewNeedScrollY
                val recyclerViewNeedVelocity = FlingUtil.getVelocityByDistance(mContext, recyclerViewScrollY)
                if (velocityY > 0) {
                    recyclerView.fling(0, recyclerViewNeedVelocity)
                } else {
                    recyclerView.fling(0, -recyclerViewNeedVelocity)
                }
            }
        }
    }


}